layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_TWL_StockIn';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			t.c.data = c.data; // 数据
			
			/**自定义的字段，流程状态**/
			t.c.onProcess = c.onProcess;
		},
        load:function () { // 加载控件
        	var h = ``
        		+ `<div class="layui-form layui-form-item" lay-filter="` + this.id + `" style="background-color: #ffffff;">`
    				+ `<label style="font-weight: 700;font-size: 18px; padding: 5px;">物料入库详情</label>`
        		+ `</div>`
        		+ `<div class="layui-table-tool" style="padding: 0; min-height: 0; background-color: #ffffff;">`
        			+ `<div class="layui-table-tool-temp">`
        				+ `<div class="layui-btn-container">`
        					+ `<a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-danger ` + (this.c.onProcess == 1 ? `layui-btn-disabled` : ``) + `" lay-event="` + (this.c.onProcess == 0 ? `event-to-delete-batch` : ``) + `"><i class="iconfont icon-delete"></i>删除</a>`
    					+ `</div>`
    				+ `</div>`
    			+ `</div>`
            	+ `<div class="layui-form layui-form-item" pane>`
            		+ `<div id="`+this.c.tableId+`">`
            			+ this.loadTable()
            		+ `</div>`
        		+ `</div>`
    		+ `</div>`;
            $('#'+this.c.id).html(h);
            this.listenBtn();
        },
        loadTable:function () { // 加载表格
            return ``
        	+ `<div class="layui-table-box">`
        		+ `<div class="layui-table-header" style="height: 300px; max-height: 300px; overflow: auto; background-color: #ffffff;">`
	            	+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="" style="margin: 0;">`
	            		+ `<thead>`
	            			+ this.loadTableOfThead()
	            		+ `</thead>`
	            		+ `<tbody>` + this.loadTableOfTbody()+`</tbody>`
	        		+ `</table>`
        		+ `</div>`
    		+ `</div>`;
        },
        loadTableOfThead:function () { // 加载表头
            var h = `<thead>`;
            h +=`<tr>`;
            h += `<th style="width:1%; text-align: center; color: #000000;"><input type="checkbox" lay-skin="primary" lay-filter="_checkbox" data-type="select_all"/></th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">字段编码</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">字段名称</th>`;
            h += `</tr>`;
            h + `</thead>`;
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = `<tbody>`;
            for(var i=0;i<t.c.data.length;i++){
	            h += `<tr>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="checkbox" lay-skin="primary" lay-filter="_checkbox" data-type="select_single" data-index="` + i + `"></td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" data-index="` + i + `" data-type="code" class="layui-input" value="` + (t.c.data[i].code ? t.c.data[i].code : '') + `" lay-filter="_input" ` + (t.c.onProcess != 0 ? `readonly="readonly"` : ``) + `></td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" data-index="` + i + `" data-type="name" class="layui-input" value="` + (t.c.data[i].name ? t.c.data[i].name : '') + `" lay-filter="_input" ` + (t.c.onProcess != 0 ? `readonly="readonly"` : ``) + `></td>`;
                h+= `</tr>`;
            }
            
            h += `</tbody>`;
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
            var domTable = $('#' + t.c.tableId);
			
            // 监听“删除”按钮
            $('a[lay-event="event-to-delete-batch"]').on('click',function (e) {

				layer.confirm('确定删除？ ', {
						btn: ['确定','取消'] //按钮
					}, function(index){
						for (var i = t.c.data.length - 1; i >= 0; i--) {
							if (t.c.data[i].checked == true) {
					        	t.c.data.splice(i, 1);
							}
						}
			        	t.reload(t.c);
						layer.close(index);
					}, function(index){
						layer.close(index);
					}
				);
            });
            
            var domTable = $('#' + t.c.tableId);
			// 监听表格中各个输入框
            domTable.find('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				
				return;
			});
            
			// 监听表格中移除按钮
            domTable.find('button[lay-filter="removeSingle"]').on('click',function (e) {
				var indexOfRow = $(this).attr('data-index');
				t.removeRow(indexOfRow);
				return;
			});
            
            // 表单控件重新加载
        	form.render();
        	
        	// 复选框勾选绑定
        	form.on('checkbox(_checkbox)', function(data){
        		var dataType = $(this).attr('data-type');
        		if (dataType == 'select_all') {
            		var checked = $(this).prop('checked');
            		// 将所有复选框置为相同选中状态
					$('input[lay-filter="_checkbox"][data-type="select_single"]').each(function(index, domEle){
						$(this).prop('checked', checked);
						var dataIndex = $(this).attr('data-index');
						t.c.data[dataIndex].checked = checked;
					});
					form.render("checkbox");
				} else {
					var checked = $(this).prop('checked');
					var dataIndex = $(this).attr('data-index');
					t.c.data[dataIndex].checked = checked;
				}
        	});
        },
        removeRow:function (index) { // 重新加载表格
        	var t = this;
        	t.c.data.splice(index, 1);
			return t.reload(t.c);
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        updateRows:function (rows) {
        	var c = this.c;
        	c.data = [];
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        }
	};
	exports('simple_table_YS_Col_init_cols', object);
});